<template>
  <div>
    <el-upload
      action="http://store-maxlen.oss-cn-guangzhou.aliyuncs.com"
      :data="dataObj"
      list-type="picture"
      :multiple="false"
      :show-file-list="showFileList"
      :file-list="fileList"
      :on-remove="handleRemove"
      :before-upload="beforeUpload"
      :on-success="handleUploadSuccess"
      :on-preview="handlePreview"
      :http-request="uploadInput">
      <el-button size="small" type="primary">点击上传</el-button>
      <div slot="tip" class="el-upload__tip">只能上传jpg文件，且不超过2MB</div>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="fileList[0].url" alt="">
    </el-dialog>
  </div>
</template>

<!--      :before-upload="beforeUpload"-->
<script>

import { getUUID } from '@/utils'
import { policy, inputFile } from '@/api/third-party/third-party'

export default {
  name: 'SingleUpload',
  props: {
    value: String
  },
  computed: {
    imageUrl() {
      return this.value
    },
    imageName() {
      if (this.value != null && this.value !== '') {
        return this.value.substr(this.value.lastIndexOf('/') + 1)
      } else {
        return null
      }
    },
    fileList() {
      return [{
        name: this.imageName,
        url: this.imageUrl
      }]
    },
    showFileList: {
      get: function() {
        return this.value !== null && this.value !== '' && this.value !== undefined
      },
      set: function(newValue) {
      }
    }
  },
  data() {
    return {
      dataObj: {
        policy: '',
        signature: '',
        key: '',
        OSSAccessKeyId: '',
        dir: '',
        host: '',
        file: null
        // callback:'',
      },
      dialogVisible: false
    }
  },
  methods: {
    emitInput(val) {
      this.$emit('input', val)
    },
    handleRemove(file, fileList) {
      this.emitInput('')
    },
    handlePreview(file) {
      this.dialogVisible = true
    },
    beforeUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    handleUploadSuccess(file) {
      console.log('上传成功...')
      this.showFileList = true
      this.fileList.pop()
      this.fileList.push({
        name: file.name,
        url: this.dataObj.host + '/' + this.dataObj.key.replace('${filename}', file.name)
      })
      this.emitInput(this.fileList[0].url)
    },
    uploadInput(file) {
      policy()
        .then(response => {
          const form = new FormData()
          form.append('OSSAccessKeyId', response.data.accessId)
          const key = response.data.dir + '/' + getUUID() + '_${filename}'
          form.append('key', key)
          form.append('policy', response.data.policy)
          form.append('signature', response.data.signature)
          form.append('file', file.file)
          inputFile(form)
            .then(() => {
              this.$message.success('上传成功')
              this.dataObj.host = response.data.host
              this.dataObj.key = key
              this.handleUploadSuccess(file.file)
            })
            .catch(() => {})
        }).catch(() => {})
    }
  }
}
</script>

